//
//  CollectionViewController.m
//  集合视图
//
//  Created by KL on 16/12/1.
//  Copyright © 2016年 KM. All rights reserved.
//

#import "CollectionViewController.h"
#import "ViewController.h"

@interface MyCell2 : UICollectionViewCell
@property (strong,nonatomic)UIImageView *iv2;

@end

@implementation MyCell2

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    _iv2 = [[UIImageView alloc]initWithFrame:self.bounds];
    [self.contentView addSubview:_iv2];    return self;
}

@end


@interface CollectionViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    NSMutableArray<UIImage *>*photo;
}
@end

@implementation CollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    photo = [NSMutableArray new];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout *flow = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flow.minimumLineSpacing = 1;
    flow.minimumInteritemSpacing =1;
    flow.itemSize = CGSizeMake(80, 80);
    [self.collectionView registerClass:[MyCell2 class] forCellWithReuseIdentifier:@"cexxx"];

    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"相册" style:UIBarButtonItemStylePlain target:self action:@selector(onDian:)];

}

- (void)onDian:(UIBarButtonItem *)btn
{
    UIImagePickerController *pi = [[UIImagePickerController alloc]init];
    pi.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    pi.delegate = self;
    [self presentViewController:pi animated:YES completion:nil];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return photo.count;
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    NSLog(@"%@",info);
    [photo addObject:info[@"UIImagePickerControllerOriginalImage"]];
    [self.collectionView reloadData];

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ViewController *vcon = [[ViewController alloc]init];
    vcon.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    vcon.im = photo;
    vcon.cur = indexPath.item;
    [self presentViewController:vcon animated:YES completion:nil];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cexxx" forIndexPath:indexPath];
    cell.iv2.image = photo[indexPath.item];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
